<?php
/* -----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----- 
	ajax.php
	
	@author Alan Berta
	@author Nick Martin
	@author Nick Major

   -----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----- 
*/

/**
 *
 */   
class maml_element extends maml_tag {

  var $doctext='Ajax event processing';
  var $id;
  var $events = array();
  var $params = array();
  var $targets = array();
  
  function begin() {
    $this->parentTag()->addElement( $this );
  }
  function end(){}

}

/**
 *
 *
 *
 */   
class maml_target extends maml_tag {

  var $doctext='Ajax event processing';
  var $OutputCache;
  
  function begin() {
    $this->OutputCache = ob_get_contents(); 
		ob_clean();
  }
  
  function end(){
    
    //
    // Retreive the event and erase.
    //
    $target = ob_get_contents(); 							
		ob_clean();
		
		//
		// return the previously saved cached output to the output buffer
		//
		echo $this->OutputCache; 
    $this->parentTag()->targets[] = $target;
  
  }

}

/**
 *
 *
 *
 */   
class maml_event extends maml_tag {

  var $doctext='Ajax event processing';
  var $OutputCache;
  var $href;
  var $event;
  
  function begin() {
    $this->OutputCache = ob_get_contents(); 
		ob_clean();
  }
  
  function end(){
    
    //
    // Retreive the event and erase.
    //
    $this->event = ob_get_contents(); 							
		ob_clean();
		
		//
		// return the previously saved cached output to the output buffer
		//
		echo $this->OutputCache; 
    $this->parentTag()->events[] = $this;
  
  }

}

/**
 *
 *
 *
 */   
class maml_parameter extends maml_tag {

  var $doctext='Ajax event processing';
  var $OutputCache;
  var $name;
  var $param;
  var $type;
  
  function begin() {
    $this->OutputCache = ob_get_contents(); 
		ob_clean();
  }
  
  function end(){
    
    //
    // Retreive the event and erase.
    //
    $this->param = ob_get_contents(); 							
		ob_clean();
		
		//
		// return the previously saved cached output to the output buffer
		//
		echo $this->OutputCache; 
    $this->parentTag()->params[] = $this;
  
  }

}

/**
 *
 *
 *
 */   
class maml_ajax extends maml_tag {
	var $doctext='Adds an AJAX helper to our page! Required for AJAX functionality.';
  var $elementList = array();
	var $trigger;
	var $target;
	var $response;
	var $event;
	
  function addElement( $newElement ) {
    $this->elementList[] = $newElement;
  } 

  //
  // Checks to see if the ajaxEngine.js file is 
  // loaded. If it is not loaded then it loads
  // it and appends it to the top.
  //
 	function begin() {
 	
//if( $GLOBALS['g_isAjaxLoaded'] == false) {
echo '<script type="text/javascript">';  
echo "
function mollyAjaxElement( target, href ) {

      var target = document.getElementById( target );
      var page = href;
      //var aI = this.addInput;

      //if( aI == true ) {

       // page = page + this.inputField[0][0] + \"=\" + this.inputField[0][1].value + \"&\";

      //} 

      var xmlHttp;
      try  {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
      }
      catch (e) {
        // Internet Explorer
      try {
        xmlHttp=new ActiveXObject(\"Msxml2.XMLHTTP\");
      } catch (e) {
      try {
        xmlHttp=new ActiveXObject(\"Microsoft.XMLHTTP\");
      }
      catch (e) {
        alert(\"Your browser does not support AJAX!\");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {

		//alert( xmlHttp.responseText );
	   target.innerHTML = xmlHttp.responseText;


    }
  }
  xmlHttp.open(\"GET\",page,true);
  xmlHttp.send(null);
  }";
			echo '</script>';
		  //$g_isAjaxLoaded=true;
		  //global $g_isAjaxLoaded;
    //}
    
	}
	
	function end() {
	   
	   //
	   // Currently only parses 1 element! Will soon beable to
	   // edit >1 elements.
	   //

	  
	  //
	  // Loop through static params that do not need dynamic content
	  // from the page (ex. != a textbox or somthing)
	  // inputCnt is a variable that keeps track of how many
	  // dynamicall generated parameters (ex. == a textbox yada yada)
	  // so that we can create the javascript array.
	  //
	  /*$inputCnt = 0;
	  $inputJSBuffer = "";
	  for( $i=0; $i<count($this->elementList[0]->params); $i++ ) {
	   if( $this->elementList[0]->params[$i]->type == 'static' ) {
	     $this->response =  $this->response . $this->elementList[0]->params[$i]->name;
	     $this->response =  $this->response . "=". $this->elementList[0]->params[$i]->param . "&";
	   } else {
       
       $inputJSBuffer = $inputJSBuffer .'el.inputField['.$inputCnt
       .']=new Array(2);el.inputField['.$inputCnt.'][0]="'
        .$this->elementList[0]->params[$i]->name
         .'";el.inputField['.$inputCnt.'][1]=document.getElementById("' 
          . $this->elementList[0]->params[$i]->param .  '");';
       $inputCnt++;

     }
	  }*/
	  
	  	if( $this->elementList[0]->events[0]->event == 'onload' ) {
   		echo '<script type="text/javascript">';
   		echo 'mollyAjaxElement( "'.$this->elementList[0]->targets[0].'", "'.$this->elementList[0]->events[0]->href.'" );';
			echo '</script>';
		} else {
		
			for( $j=0; $j<count( $this->elementList ); $j++ ) {

	  			//$this->response = $this->elementList[$j]->events[0]->href . "?";
	  			//$this->trigger = $this->elementList[$j]->id;
	  			//$this->target = $this->elementList[$j]->targets[0];
	
	  			$cache = ob_get_contents(); 
	  			ob_clean();

	  			$loc = strpos( $cache, $this->elementList[$j]->id );
	  		
	  			$first = substr( $cache, 0, $loc + strlen($this->elementList[$j]->id) + 1 ); 
	 			$sec = substr( $cache, $loc + strlen($this->elementList[$j]->id) + 1 ); 
	 		
				echo $first;
				echo " ".$this->elementList[$j]->events[0]->event."=\"mollyAjaxElement('".$this->elementList[$j]->targets[0]."','"
					.$this->elementList[$j]->events[0]->href."');\" ";
				echo $sec; 		
				
			}
	 			 		
	 	}
	  
	  //$first = substr( $cache, 0 , $loc+strlen($this->trigger)+2 );
	  //$second = substr( $cache, $loc+strlen($this->trigger)+2 );
	  
	  //echo $first . " HELLO I MADE IT! " . $second;



	  		


	}
	
}


?>
